package cn.wolfcode.rocketmq._07_filter;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {

    public static void main(String[] args) throws Exception {
        //创建消费者对象
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("consumer-demo");
        //绑定nameserver地址
        defaultMQPushConsumer.setNamesrvAddr("localhost:9876");
        //监听要消费的主题
        defaultMQPushConsumer.subscribe("07-filter", "TagA");
        //写监听的方法监听消息
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
           //如果监听到指定topic中有消息那么会执行consumeMessage方法
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("消息的内容为:"+new String(messageExt.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //启动消费者
        defaultMQPushConsumer.start();

    }
}
